/*
 * Copyright (C) 2004-2010  Autodesk, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of version 2.1 of the GNU Lesser
 * General Public License as published by the Free Software Foundation.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include "stdafx.h"
#include "FdoCMSXmlGlobals.h"
#include "FdoCMSYahooGetMap.h"
#include <FdoCommonStringUtil.h>
#include <OWS/FdoOwsGlobals.h>

FdoCMSYahooGetMap::FdoCMSYahooGetMap ():
        m_ParamsURL(L"")
{
}

FdoCMSYahooGetMap* FdoCMSYahooGetMap::Create (FdoString* url)
{
    return new FdoCMSYahooGetMap (url);
}

FdoCMSYahooGetMap::FdoCMSYahooGetMap (FdoString* url) : 
      m_ParamsURL(url ? url : L"") 
{
}

//FdoCMSYahooGetMap::FdoCMSYahooGetMap ():
//        m_X(0),m_Y(0),m_Z(0)
//{
//}
//
FdoCMSYahooGetMap* FdoCMSYahooGetMap::Create (FdoSize tileX, FdoSize tileY, int zoomLevel, FdoDouble ver)
{
    return new FdoCMSYahooGetMap (tileX,tileY,zoomLevel,ver);
}

FdoCMSYahooGetMap::FdoCMSYahooGetMap (FdoSize tileX, FdoSize tileY, int zoomLevel, FdoDouble ver)
{
    int z = zoomLevel + 1;
    FdoInt32 num4 = (1 << zoomLevel) >> 1; // 2^(zoomLevel - 1)

    int x = (int)tileX;
    int y = (int)tileY;

    if(y < num4)
        y = (num4 - y) - 1;
    else
        y = ((y + 1) - num4)*(-1);

    FdoStringP ret = L"";

    ret += FdoCMSXmlGlobals::CMSYahooRequestVersion;
    ret += FdoOwsGlobals::Equal;
    ret += FdoStringP::Format(L"%f", ver);

    ret += FdoOwsGlobals::And;
    ret += FdoCMSXmlGlobals::CMSYahooRequestType;

    ret += FdoOwsGlobals::And;
    ret += FdoCMSXmlGlobals::CMSYahooRequestRadius;

    ret += FdoOwsGlobals::And;
    ret += FdoCMSXmlGlobals::CMSYahooRequestTileX;
    ret += FdoOwsGlobals::Equal;
    ret += FdoStringP::Format(L"%d", x);

    ret += FdoOwsGlobals::And;
    ret += FdoCMSXmlGlobals::CMSYahooRequestTileY;
    ret += FdoOwsGlobals::Equal;
    ret += FdoStringP::Format(L"%d", y);

    ret += FdoOwsGlobals::And;
    ret += FdoCMSXmlGlobals::CMSYahooRequestZoom;
    ret += FdoOwsGlobals::Equal;
    ret += FdoStringP::Format(L"%d", z);

    // Translate to a valid HTTP request
    ret = ret.Replace(L" ", L"%20"); 

    m_ParamsURL = ret;
}

      
FdoCMSYahooGetMap::~FdoCMSYahooGetMap ()
{
}


FdoStringP FdoCMSYahooGetMap::EncodeKVP()
{

     //http://maps3.yimg.com/ae/ximg?v=1.9&t=a&r=1&x=1&y=0&z=5
     //http://maps3.yimg.com/hx/tl?v=4.3&t=a&r=1&x=4&y=0&z=5
     return m_ParamsURL;

}

FdoStringP FdoCMSYahooGetMap::EncodeXml()
{
    // TODO: shall we support this?
    return L"";
}

